/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess.Utilities
*        文件名:             SqlHelper.cs
*        创建系统时间:       2012/11/2 19:07:01
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess.Utilities
{
    using System;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;

    public class SqlHelper : BaseDbHelper, IDbHelper
    {
        public SqlHelper()
        {
            base.FileName = "SqlHelper.txt";
        }

        public SqlHelper(string connectionString)
        {
            base.dbConnection.ConnectionString = connectionString;
        }

        public override void AttachParameters(IDbCommand dbCommand, IDataParameter[] dataParameter)
        {
            for (int i = 0; i < dataParameter.Length; i++)
            {
                ((SqlCommand)dbCommand).Parameters.Add((SqlParameter)dataParameter[i]);
            }
        }

        public string GetDBDateTime()
        {
            string sqlQuery = " SELECT " + this.GetDBNow();
            base.Open();
            string str2 = base.ExecuteScalar(CommandType.Text, sqlQuery, null).ToString();
            base.Close();
            return str2;
        }

        public string GetDBNow()
        {
            return " Getdate() ";
        }

        public string GetParameter(string parameter)
        {
            return (" @" + parameter + " ");
        }

        public DbProviderFactory Instance()
        {
            return SqlClientFactory.Instance;
        }

        public DbParameter MakeInParam(string targetFiled, object targetValue)
        {
            SqlParameter parameter = null;
            if (targetValue != null)
            {
                if (targetValue is string)
                {
                    parameter = new SqlParameter("@" + targetFiled, SqlDbType.VarChar);
                }
                else if (targetValue is bool)
                {
                    parameter = new SqlParameter("@" + targetFiled, SqlDbType.Bit);
                }
                else if (targetValue is DateTime)
                {
                    parameter = new SqlParameter("@" + targetFiled, SqlDbType.DateTime);
                }
                else if (targetValue is int)
                {
                    parameter = new SqlParameter("@" + targetFiled, SqlDbType.Int);
                }
                else if (targetValue is long)
                {
                    parameter = new SqlParameter("@" + targetFiled, SqlDbType.BigInt);
                }
                else if (targetValue is double)
                {
                    parameter = new SqlParameter("@" + targetFiled, SqlDbType.Decimal);
                }
                else if (targetValue is byte[])
                {
                    parameter = new SqlParameter("@" + targetFiled, SqlDbType.Binary);
                }
                else
                {
                    parameter = new SqlParameter("@" + targetFiled, SqlDbType.VarChar);
                }
                parameter.Value = targetValue;
            }
            return parameter;
        }

        public DbParameter MakeInParam(string paramName, DbType DbType, int Size, object Value)
        {
            return this.MakeParam(paramName, DbType, Size, ParameterDirection.Input, Value);
        }

        public DbParameter MakeOutParam(string paramName, DbType DbType, int Size)
        {
            return this.MakeParam(paramName, DbType, Size, ParameterDirection.Output, null);
        }

        public DbParameter MakeParam(string paramName, DbType DbType, int Size, ParameterDirection Direction, object Value)
        {
            SqlParameter parameter;
            if (Size > 0)
            {
                parameter = new SqlParameter(paramName, (SqlDbType)DbType, Size);
            }
            else
            {
                parameter = new SqlParameter(paramName, (SqlDbType)DbType);
            }
            parameter.Direction = Direction;
            if ((Direction != ParameterDirection.Output) || (Value != null))
            {
                parameter.Value = Value;
            }
            return parameter;
        }

        public DbParameter[] MakeParameters(string targetFiled, object targetValue)
        {
            DbParameter[] parameterArray = null;
            if ((targetFiled != null) && (targetValue != null))
            {
                parameterArray = new DbParameter[] { this.MakeInParam(targetFiled, targetValue) };
            }
            return parameterArray;
        }

        public DbParameter[] MakeParameters(string[] targetFileds, object[] targetValues)
        {
            DbParameter[] parameterArray = new DbParameter[targetFileds.Length];
            if ((targetFileds != null) && (targetValues != null))
            {
                parameterArray = new DbParameter[targetFileds.Length];
                for (int i = 0; i < targetFileds.Length; i++)
                {
                    parameterArray[i] = this.MakeInParam(targetFileds[i], targetValues[i]);
                }
            }
            return parameterArray;
        }

        public string PlusSign(params string[] values)
        {
            string str = string.Empty;
            for (int i = 0; i < values.Length; i++)
            {
                str = str + values[i] + " + ";
            }
            if (!string.IsNullOrEmpty(str))
            {
                return str.Substring(0, str.Length - 3);
            }
            return " + ";
        }

        public string SqlSafe(string value)
        {
            value = value.Replace("'", "''");
            return value;
        }
    }
}